using System;
using System.Collections.Generic;
using System.Linq;

namespace PokerStar.Core.Rules
{
    public class FourOfKindRule : BasicRule
    {
        public FourOfKindRule(IRule next)
            : base(next)
        {
        }


        protected override GameResult Match(RuleContext context)
        {
            //foreach (var group in cards.GroupBy(p => p.Number))
            //{
            //    if (group.Count() == 4)
            //    {
            //        var rs = new FourOfKindGameResult(group.Key);
            //        rs.Cards = group.ToList();
            //        rs.Cards.Add(cards.
            //            Where(p => p.Number != group.Key).
            //            OrderByDescending(p => p.Number, new CardNumberComparer()).
            //            First());
            //        return rs;
            //    }
            //}

            if (context.GroupByNumber.First().Count() == 4)
            {
                var group = context.GroupByNumber.First();
                var rs = new FourOfKindGameResult(group.Key);
                rs.Cards = group.ToList();
                rs.Cards.Add(context.Ordinal.
                    Where(p => p.Number != group.Key).
                    OrderByDescending(p => p.Number, new CardNumberComparer()).
                    First());
                return rs;
            }

            return null;
        }
    }

    public class FourOfKindGameResult : GameResult
    {
        public FourOfKindGameResult(char number)
        {
            Evidence = new List<int> { CardNumberComparer.Card2Int(number), 0, 0, 0, 0 };
        }

        public override ResultLevel Level
        {
            get { return ResultLevel.FourOfKind; }
        }

        public override string ToString()
        {
            return string.Format("Four of a kind -> {0} {1} {2} {3} {4}", Cards[0], Cards[1], Cards[2], Cards[3], Cards[4]);
        }
    }
}